package com.hqyj.shop.service.impl;

import com.hqyj.shop.pojo.Car;
import com.hqyj.shop.mapper.CarMapper;
import com.hqyj.shop.service.CarService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.shop.util.JwtUtil;
import com.hqyj.shop.util.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
 * @since 2024-10-14 01:58:48
 */
@Service
@Slf4j
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements CarService {

    //注入redis工具类
    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    @Override
    public ResultVo addCar(Car c, HttpServletRequest request) {
        //获取Token
        String token = request.getHeader("Authorization");
        //获取用户名
        String name = JwtUtil.getClaimByName(token,"userName").asString();
        //设置userId
        String userId = JwtUtil.getAudience(token);
        c.setUserId(Integer.parseInt(userId));
        try{
            redisTemplate.opsForHash().put("car:"+name,c.getGoodsId()+"",c);
            //计算你的购物车的数量
            Map<Object,Object> m = redisTemplate.opsForHash().entries("car:"+name);
            log.info("购物车添加成功");
            return ResultVo.success("添加购物车成功",m.size());
        }catch (Exception e){
            e.printStackTrace();
            log.info("购物车添加失败");
            return ResultVo.error("购物车添加失败");
        }
    }

    @Override
    public ResultVo delCar(Car c, HttpServletRequest request) {
        //获取Token
        String token = request.getHeader("Authorization");
        //获取用户名
        String name = JwtUtil.getClaimByName(token,"userName").asString();
        try{
            redisTemplate.opsForHash().delete("car:"+name,c.getGoodsId()+"");
            log.info("购物车删除成功");
            return ResultVo.success("购物车删除成功");
        }catch (Exception e){
            e.printStackTrace();
            log.info("购物车删除失败");
            return ResultVo.error("购物车删除失败");
        }
    }

    @Override
    public ResultVo updateCar(Car c, HttpServletRequest request) {

        //获取Token
        String token = request.getHeader("Authorization");
        //获取用户名
        String name = JwtUtil.getClaimByName(token,"userName").asString();
        try{
            //先删除
            redisTemplate.opsForHash().delete("car:"+name,c.getGoodsId()+"");
            //再添加
            redisTemplate.opsForHash().put("car:"+name,c.getGoodsId()+"",c);
            log.info("购物车修改成功");
            return ResultVo.success("购物车修改成功");
        }catch (Exception e){
            e.printStackTrace();
            log.info("购物车修改失败");
            return ResultVo.error("购物车修改失败");
        }
    }

    @Override
    public ResultVo queryCar(HttpServletRequest request) {
        //获取Token
        String token = request.getHeader("Authorization");
        //获取用户名
        String name = JwtUtil.getClaimByName(token,"userName").asString();
        try{
            Map<Object,Object> m = redisTemplate.opsForHash().entries("car:"+name);
            if(m.size()>0){
                //创建一个集合
                List<Car> list = new ArrayList<>();
                Set<Map.Entry<Object,Object>> set =m.entrySet();
                //创建迭代器
                Iterator<Map.Entry<Object,Object>> iterator =set.iterator();
                while(iterator.hasNext()){
                    //获取集合的一条数据
                    Map.Entry<Object,Object> data =iterator.next();
                    Car c =(Car)data.getValue();
                    list.add(c);
                }
                log.info("购物车查询成功");
                return ResultVo.success(list);
            }else{
                return ResultVo.error("没有查询到数据");
            }

        }catch (Exception e){
            e.printStackTrace();
            log.info("购物车查询失败");
            return ResultVo.error("购物车查询失败");
        }
    }
}
